#include "user_communication.h"
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "stdint.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

void uart_recv_handle(void)
{
	int32_t head = 0;
	if (uart_recv_flag.head != uart_recv_flag.tail) {
		// hexdump(uart_recv_flag.data[uart_recv_flag.head].data, uart_recv_flag.data[uart_recv_flag.head].len);
		head = uart_send_flag.head;
		if (head - 1 >= 0) {
			// printf("%s %d send head:%d\r\n", __func__, __LINE__, uart_send_flag.head - 1);
			user_zw101_parse(uart_send_flag.data[uart_send_flag.head - 1].data, uart_send_flag.data[uart_send_flag.head - 1].len);
		} else {
			// printf("%s %d send head:%d is end\r\n", __func__, __LINE__, uart_send_flag.head);
			user_zw101_parse(uart_send_flag.data[UART_SEND_MAX - 1].data, uart_send_flag.data[UART_SEND_MAX - 1].len);
		}
		uart_recv_flag.head = (uart_recv_flag.head + 1) % UART_RECV_MAX;
	}	
}

void user_communication_task(void *arg)
{
    printf("%s %d start\r\n",__func__,__LINE__);

    while (1) {
		uart_recv_handle();
		user_zw101_handle();
		vTaskDelay(10);
    }
}